package com.wing.productionPlan.controller.admin;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.enums.Idempotent;
import com.wing.common.enums.RequestKeyParam;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProProductDeliveryOrder;
import com.wing.productionPlan.model.response.ProProductDeliveryOrderVo;
import com.wing.productionPlan.service.ProProductDeliveryOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 提货订单
 *
 * @author 25923
 */
@Slf4j
@Validated
@Api(value = "Admin-提货订单", tags = "#/product/delivery/orderInfo")
@RestController("proProductDeliveryOrderController")
@RequestMapping("delivery/admin/orderInfo")
@RequiredArgsConstructor
public class ProProductDeliveryOrderController {

    @Autowired
    private ProProductDeliveryOrderService proProductDeliveryOrderService;

    @ApiOperation(value = "查询提货订单列表", notes = "搜索-提货订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProProductDeliveryOrderVo> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String customerName
            , String purchaseName
            , String orderNo
            ,String startTime
            ,String endTime) {
        Page<ProProductDeliveryOrderVo> pg = new Page<>(page, limit);
        Map<String,Object> map = new HashMap<>();
        map.put("customerName", customerName);
        map.put("purchaseName", purchaseName);
        map.put("orderNo", orderNo);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        Page<ProProductDeliveryOrderVo> list = proProductDeliveryOrderService.pageList4app(pg, map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加提货订单", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    @Idempotent(keyPrefix = "deliveryAdd")
    public JsonResult add(@Validated(ProProductDeliveryOrder.class) ProProductDeliveryOrder proProductDeliveryOrder) {
        if (proProductDeliveryOrderService.add(proProductDeliveryOrder) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改提货订单", notes = "修改")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @Idempotent(keyPrefix = "deliveryUpdate")
    @PostMapping("/update")
    public JsonResult update(@Validated(ProProductDeliveryOrder.class) ProProductDeliveryOrder proProductDeliveryOrder) {
        if (proProductDeliveryOrderService.updateInfo(proProductDeliveryOrder) > 0) {
            return JsonResult.success("修改成功");
        }
        return JsonResult.fail("修改失败");
    }

}
